package com.juzhi.dvrapp.activity;

import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.transition.Explode;
import android.view.View;
import android.view.Window;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.juzhi.dvrapp.R;
import com.juzhi.dvrapp.bean.Device;
import com.juzhi.dvrapp.manager.DeviceManager;
import com.juzhi.dvrapp.utils.Config;
import com.juzhi.dvrapp.utils.MLog;
import com.juzhi.dvrapp.utils.Urls;
import com.juzhi.dvrapp.videoplay.builder.GSYVideoOptionBuilder;
import com.juzhi.dvrapp.videoplay.listener.LockClickListener;
import com.juzhi.dvrapp.videoplay.listener.StandardVideoAllCallBack;
import com.juzhi.dvrapp.videoplay.utils.OrientationUtils;
import com.juzhi.dvrapp.videoplay.video.StandardGSYVideoPlayer;
import com.juzhi.dvrapp.videoplay.video.base.GSYVideoPlayer;
import com.juzhi.dvrapp.widget.LandLayoutVideo;

import butterknife.BindView;

public class DevicePlayAndMapActivity extends BaseAcitivity implements OnMapReadyCallback {

    @BindView(R.id.video_player)
    LandLayoutVideo videoPlayer;

    private boolean isPlay;
    private boolean isPause;

    private OrientationUtils orientationUtils;
    private Device mDevice;
    private String thumbUrl;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            getWindow().requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);
            getWindow().setEnterTransition(new Explode());
            getWindow().setExitTransition(new Explode());
        }
        super.onCreate(savedInstanceState);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_device_play_and_map;
    }

    @Override
    protected void initView() {
        String mSsid = getIntent().getStringExtra(Config.SSID);
        DeviceManager deviceManager = DeviceManager.getDeviceManager(this);
        mDevice = deviceManager.getDevice(mSsid);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        thumbUrl = Urls.URL_CMS_GET_DEVICE_THUMB + mSsid + "/" + mSsid + "_1.JPEG";
        playVideo();
    }

    private void playVideo() {
        orientationUtils = new OrientationUtils(this, videoPlayer);
        orientationUtils.setEnable(false);  //禁止自动旋转
        videoPlayer.getBackButton().setVisibility(View.GONE);
        videoPlayer.loadCoverImage(thumbUrl, R.mipmap.logo);

        GSYVideoOptionBuilder gsyVideoOption = new GSYVideoOptionBuilder();
        gsyVideoOption.setIsTouchWiget(true)
                .setRotateViewAuto(false)
                .setLockLand(false)
                .setShowFullAnimation(false)
                .setNeedLockFull(true)
                .setIsLine(true)
                .setCacheWithPlay(false)
                .setVideoTitle(mDevice.getDeviceModel().getDeviceName())
                .setDeviceSsid(mDevice.getDeviceModel().getSsid())
                .setStandardVideoAllCallBack(new StandardVideoAllCallBack() {
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        MLog.e("***** onPrepared **** " + objects[0]);
                        MLog.e("***** onPrepared **** " + objects[1]);
                        //开始播放了才能旋转和全屏
                        orientationUtils.setEnable(true);
                        isPlay = true;
                    }

                    @Override
                    public void onClickStartIcon(String url, Object... objects) {

                    }

                    @Override
                    public void onClickStartError(String url, Object... objects) {

                    }

                    @Override
                    public void onClickStop(String url, Object... objects) {

                    }

                    @Override
                    public void onClickStopFullscreen(String url, Object... objects) {

                    }

                    @Override
                    public void onClickResume(String url, Object... objects) {

                    }

                    @Override
                    public void onClickResumeFullscreen(String url, Object... objects) {

                    }

                    @Override
                    public void onClickSeekbar(String url, Object... objects) {

                    }

                    @Override
                    public void onClickSeekbarFullscreen(String url, Object... objects) {

                    }

                    @Override
                    public void onAutoComplete(String url, Object... objects) {

                    }

                    @Override
                    public void onEnterFullscreen(String url, Object... objects) {
                        MLog.e("***** onEnterFullscreen **** " + objects[0]);//title
                        MLog.e("***** onEnterFullscreen **** " + objects[1]);//当前全屏player
                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        MLog.e("***** onQuitFullscreen **** " + objects[0]);//title
                        MLog.e("***** onQuitFullscreen **** " + objects[1]);//当前非全屏player
                        if (orientationUtils != null) {
                            orientationUtils.backToProtVideo();
                        }
                    }

                    @Override
                    public void onQuitSmallWidget(String url, Object... objects) {

                    }

                    @Override
                    public void onEnterSmallWidget(String url, Object... objects) {

                    }

                    @Override
                    public void onTouchScreenSeekVolume(String url, Object... objects) {

                    }

                    @Override
                    public void onTouchScreenSeekPosition(String url, Object... objects) {

                    }

                    @Override
                    public void onTouchScreenSeekLight(String url, Object... objects) {

                    }

                    @Override
                    public void onPlayError(String url, Object... objects) {

                    }

                    @Override
                    public void onClickStartThumb(String url, Object... objects) {

                    }

                    @Override
                    public void onClickBlank(String url, Object... objects) {

                    }

                    @Override
                    public void onClickBlankFullscreen(String url, Object... objects) {

                    }
                })
                .setLockClickListener(new LockClickListener() {
                    @Override
                    public void onClick(View view, boolean lock) {
                        if (orientationUtils != null) {
                            //配合下方的onConfigurationChanged
                            orientationUtils.setEnable(!lock);
                        }
                    }
                }).build(videoPlayer);

        videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //直接横屏
                orientationUtils.resolveByClick();

                //第一个true是否需要隐藏actionbar，第二个true是否需要隐藏statusbar
                videoPlayer.startWindowFullscreen(DevicePlayAndMapActivity.this, true, true);
            }
        });
        videoPlayer.startPlayLogic();
    }

    @Override
    public void onBackPressed() {
        if (orientationUtils != null) {
            orientationUtils.backToProtVideo();
        }
        if (StandardGSYVideoPlayer.backFromWindowFull(this)) {
            return;
        }
        super.onBackPressed();
    }

    @Override
    protected void onPause() {
        getCurPlay().release();
        super.onPause();
        isPause = true;
    }

    @Override
    protected void onResume() {
        getCurPlay().onVideoResume();
        super.onResume();
        isPause = false;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isPlay) {
            getCurPlay().release();
        }
        //GSYPreViewManager.instance().releaseMediaPlayer();
        if (orientationUtils != null)
            orientationUtils.releaseListener();
    }

    private GSYVideoPlayer getCurPlay() {
        if (videoPlayer.getFullWindowPlayer() != null) {
            return videoPlayer.getFullWindowPlayer();
        }
        return videoPlayer;
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        //如果旋转了就全屏
        if (isPlay && !isPause) {
            if (newConfig.orientation == ActivityInfo.SCREEN_ORIENTATION_USER) {
                if (!videoPlayer.isIfCurrentIsFullscreen()) {
                    videoPlayer.startWindowFullscreen(DevicePlayAndMapActivity.this, true, true);
                }
            } else {
                //新版本isIfCurrentIsFullscreen的标志位内部提前设置了，所以不会和手动点击冲突
                if (videoPlayer.isIfCurrentIsFullscreen()) {
                    StandardGSYVideoPlayer.backFromWindowFull(this);
                }
                if (orientationUtils != null) {
                    orientationUtils.setEnable(true);
                }
            }
        }
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        GoogleMap mMap = googleMap;

        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(22.582200, 113.925992), 15));

    }
}
